package com.cn.xiaonuo.slbfinances.slborders.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.constant.Status;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.ExportExcelUtils;
import com.cn.xiaonuo.common.vo.ExcelData;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.slborders.entity.InOutOrder;
import com.cn.xiaonuo.slbfinances.slborders.entity.SlOrder;
import com.cn.xiaonuo.slbfinances.slborders.entity.SlOrderDetail;
import com.cn.xiaonuo.slbfinances.slborders.mapper.CmOrderMapper;
import com.cn.xiaonuo.slbfinances.slborders.param.SlbOrderParam;
import com.cn.xiaonuo.slbfinances.slborders.service.ISlbOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SlbOrderServiceImpl extends ServiceImpl<CmOrderMapper, SlOrder> implements ISlbOrderService {

    @Resource
    private CmOrderMapper cmOrderMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage selectYunMao(SlbOrderParam param) {
        BusinessMessage message = new BusinessMessage();
        Integer pageSize = param.getPageSize();
        try {
            if (null == param.getPageNo() || param.getPageNo() < 1) {
                param.setPageNo(1);
            }
            if (null == pageSize || pageSize <= 1) {
                param.setPageSize(10);
            }
            if (null == param.getQrState()) {
                param.setQrState(3);
            }
            if (param.getTimeFlg() == null) {
                param.setTimeFlg(0);
            }
            if (param.getPaymentChannel() == null) {
                param.setPaymentChannel(0);
            }
            // 设置分页参数
            PageHelper.startPage(param.getPageNo(), param.getPageSize());
            List<Map<String, Object>> list = cmOrderMapper.yunList(param);
            PageInfo pageinfo = new PageInfo<>(list);
            // Map sum = this.cmOrderMapper.yunListSum(param);
            Map<String, Object> data = new HashMap<>();
            data.put("pageInfo", pageinfo);
            // data.put("add", sum);
            message.setMessage("查询成功");
            message.setSuccess(true);
            message.setData(data);
        } catch (Exception e) {
            message.setMessage("无查询结果！");
            log.error("查询失败:{}", e);
            e.printStackTrace();
        }
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage selectDetailInfo(Integer orderId) {
        BusinessMessage message = new BusinessMessage();
        try {
            if (null != orderId) {
                List<SlOrderDetail> maplist = cmOrderMapper.listOrderDetail(orderId);
                message.setData(maplist);
                message.setSuccess(true);
                message.setMessage("操作成功");
            } else {
                message.setMessage("操作失败");
                message.setSuccess(false);
                return message;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }

    /**
     * 搜了宝订单管理--匀贸入账列表
     *
     * @param param
     * @return
     */
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage inOutOrderList(SlbOrderParam param) {
        BusinessMessage message = new BusinessMessage();
        Integer pageNum = param.getPageNo();
        Integer pageSize = param.getPageSize();
        if (null == param.getPageNo() || param.getPageNo() < 1) {
            param.setPageNo(1);
        }
        if (null == pageSize || pageSize <= 1) {
            param.setPageSize(10);
        }
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        List<InOutOrder> list = this.cmOrderMapper.inOutOrderList(param);
        PageInfo<InOutOrder> pageInfo = new PageInfo<>(list);
        message.setMessage("查询成功");
        message.setSuccess(true);
        message.setData(pageInfo);
        return message;
    }

    /**
     * 导出
     *
     * @param slbOrderParam
     * @author luangyang
     * @date 2022-04-15 16:08
     */
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void inOutOrderExcel(HttpServletResponse response, SlbOrderParam slbOrderParam) throws Exception {
        List<InOutOrder> list = cmOrderMapper.inOutOrderList(slbOrderParam);
        try {
            List<List<Object>> rows = new ArrayList();
            ExcelData data = new ExcelData();
            data.setName("进出货订单导出");//sheet表名称
            //表头
            List<String> titles = new ArrayList();
            titles.add("产业公司（出货方）");
            titles.add("产业公司ID（出货方）");
            titles.add("加盟店（进货方）");
            titles.add("加盟店ID（进货方）");
            titles.add("加盟店用户ID（进货方）");
            titles.add("服务中心（上级）");
            titles.add("服务中心ID");
            titles.add("订单ID");
            titles.add("进货单号");
            titles.add("主订单号");
            titles.add("商品名称");
            titles.add("数量");
            titles.add("进货单价");
            titles.add("进价总金额");
            titles.add("现金实付款");
            titles.add("订单状态");
            titles.add("支付方式");
            titles.add("收件人");
            titles.add("收货地址");
            titles.add("进货时间");
            titles.add("发货时间");
            titles.add("收货时间");
            data.setTitles(titles);
            for (InOutOrder inOutOrder : list) {
                List<Object> row1 = new ArrayList();
                row1.add(inOutOrder.getShopName());
                row1.add(inOutOrder.getShopId());
                row1.add(inOutOrder.getJoinName());
                row1.add(inOutOrder.getJoinId());
                row1.add(inOutOrder.getUserId());
                row1.add(inOutOrder.getServiceName());
                row1.add(inOutOrder.getServiceId());
                row1.add(inOutOrder.getOrderId());
                row1.add(inOutOrder.getSerialNumber());
                row1.add(inOutOrder.getOrderNumber());
                row1.add(inOutOrder.getProductName());
                row1.add(inOutOrder.getQuantity());
                row1.add(inOutOrder.getPrice());
                row1.add(inOutOrder.getTotalAmount());
                row1.add(inOutOrder.getActualAmount());
                if (inOutOrder.getShippingState() != null) {
                    if (inOutOrder.getShippingState() == 0) {
                        row1.add("待支付");
                    } else if (inOutOrder.getShippingState() == 3) {
                        row1.add("待发货");
                    } else if (inOutOrder.getShippingState() == 4) {
                        row1.add("待收货");
                    } else if (inOutOrder.getShippingState() == 5) {
                        row1.add("已完成/待评价");
                    } else if (inOutOrder.getShippingState() == 6) {
                        row1.add("已评价");
                    } else if (inOutOrder.getShippingState() == 7) {
                        row1.add("退款成功");
                    } else if (inOutOrder.getShippingState() == 8) {
                        row1.add("申请退款");
                    } else {
                        row1.add("");
                    }
                }
                if (inOutOrder.getPaymentChannel() != null) {
                    if (inOutOrder.getShippingState() != 0) {
                        if (inOutOrder.getPaymentChannel() == 1) {
                            row1.add("微信");
                        } else if (inOutOrder.getPaymentChannel() == 2) {
                            row1.add("支付宝");
                        } else if (inOutOrder.getPaymentChannel() == 3) {
                            row1.add("paypal");
                        } else if (inOutOrder.getPaymentChannel() == 5) {
                            row1.add("线下付款");
                        } else if (inOutOrder.getPaymentChannel() == 10) {
                            row1.add("纯购物劵");
                        } else if (inOutOrder.getPaymentChannel() == 11) {
                            row1.add("支付宝+购物劵");
                        } else if (inOutOrder.getPaymentChannel() == 12) {
                            row1.add("微信+购物劵");
                        } else if (inOutOrder.getPaymentChannel() == 13) {
                            row1.add("搜了付");
                        } else if (inOutOrder.getPaymentChannel() == 15) {
                            row1.add("微信+线下支付");
                        } else if (inOutOrder.getPaymentChannel() == 16) {
                            row1.add("支付宝+线下支付");
                        } else if (inOutOrder.getPaymentChannel() == 17) {
                            row1.add("银联支付");
                        } else if (inOutOrder.getPaymentChannel() == 18) {
                            row1.add("银联+购物券");
                        }else if (inOutOrder.getPaymentChannel() == 104) {
                            row1.add("货款支付");
                        }else if (inOutOrder.getPaymentChannel() == 19) {
                            row1.add("杉德支付");
                        }else {
                            row1.add("其他");
                        }
                    } else {
                        row1.add("");
                    }
                } else {
                    row1.add("");
                }
                row1.add(inOutOrder.getConsigneeName());
                row1.add(inOutOrder.getDetailed());
                row1.add(inOutOrder.getCreateTime());
                row1.add(inOutOrder.getShippingTime());
                row1.add(inOutOrder.getConfirmReceiptTime());

                rows.add(row1);
            }
            data.setRows(rows);
            ExportExcelUtils.exportExcel(response, "招商联系信息导出.xlsx", data);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void selectYunMaoExcel(HttpServletResponse response, SlbOrderParam param) {
        if (ObjectUtils.isEmpty(param.getEndTime()) || ObjectUtils.isEmpty(param.getStatTime())){
         return;
        }
        List<Map<String, Object>> list = cmOrderMapper.selectYunMaoExcel(param);
        try {
            List<List<Object>> rows = new ArrayList();
            ExcelData data = new ExcelData();
            data.setName("云贸商城商家订单导出");//sheet表名称
            //表头
            List<String> titles = new ArrayList();
            titles.add("商家名称");
            titles.add("商家类型");
            titles.add("商家ID");
            titles.add("商家用户ID");
            titles.add("服务中心(上级)");
            titles.add("服务中心ID");
            titles.add("主订单号");
            titles.add("订单号");
            titles.add("订单编号");
            titles.add("订单类型");
            titles.add("商品名称");
            titles.add("数量");
            titles.add("单价");
            titles.add("进货价");
            titles.add("订单总金额");
            titles.add("进价总金额");
            titles.add("用户实付款");
            titles.add("购物券(交)");
            titles.add("平台服务费现金");
            titles.add("平台服务费购物券");
            titles.add("订单状态");
            titles.add("支付方式");
            titles.add("购买人ID");
            titles.add("收件人");
            titles.add("收货地址");
            titles.add("联系方式");
            titles.add("下单时间");
            titles.add("支付时间");
            titles.add("收货时间");

            data.setTitles(titles);
            Map<String, Object> map = new HashMap<>();
            for (Map yunList : list) {
                List<Object> row1 = new ArrayList();
                row1.add(yunList.get("shop_name"));
                row1.add(yunList.get("shopType"));
                row1.add(yunList.get("shopId"));
                row1.add(yunList.get("shop_uid"));
                row1.add(yunList.get("serviceName"));
                row1.add(yunList.get("serviceId"));
                row1.add(yunList.get("order_number"));
                row1.add(yunList.get("order_id"));
                row1.add(yunList.get("serial_number"));
                if (yunList.get("isPubGoods") != null) {
                    if (Convert.toInt(yunList.get("isPubGoods")) == 0) {
                        row1.add("普通商品");
                    } else if (Convert.toInt(yunList.get("isPubGoods")) == 1) {
                        row1.add("一县一品订单");
                    } else if (Convert.toInt(yunList.get("isPubGoods")) == 2) {
                        row1.add("厂家进货商品");
                    } else {
                        row1.add("");
                    }
                } else {
                    row1.add("");
                }
                row1.add(yunList.get("productName"));
                row1.add(yunList.get("quantity"));
                row1.add(yunList.get("price"));
                row1.add(yunList.get("slb"));
                if (yunList.get("serial_number").equals(map.get("serial_number"))) {
                    row1.add(0);
                } else {
                    row1.add(yunList.get("total"));
                }
                row1.add(yunList.get("costTotal"));
                if (yunList.get("serial_number").equals(map.get("serial_number"))) {
                    row1.add(0);
                } else {
                    row1.add(yunList.get("total_amount"));
                }
                row1.add(yunList.get("totalJybGwj"));
                row1.add(yunList.get("total_tc"));
                row1.add(yunList.get("total_gwjTc"));
                if (yunList.get("shippingState") != null) {
                    if (Convert.toInt(yunList.get("shippingState")) == 0) {
                        row1.add("待支付");
                    } else if (Convert.toInt(yunList.get("shippingState")) == 1) {
                        row1.add("待上级确认");
                    } else if (Convert.toInt(yunList.get("shippingState")) == 3) {
                        row1.add("待发货");
                    } else if (Convert.toInt(yunList.get("shippingState")) == 4) {
                        row1.add("待收货/已发货");
                    } else if (Convert.toInt(yunList.get("shippingState")) == 5) {
                        row1.add("已完成/未评价");
                    } else if (Convert.toInt(yunList.get("shippingState")) == 6) {
                        row1.add("已评价");
                    } else if (Convert.toInt(yunList.get("shippingState")) == 7) {
                        row1.add("退款成功");
                    } else if (Convert.toInt(yunList.get("shippingState")) == 8) {
                        row1.add("申请退款");
                    } else {
                        row1.add("");
                    }
                }
                if (yunList.get("payment_channel") != null) {
                    if (Convert.toInt(yunList.get("payment_channel")) != 0) {
                        if (Convert.toInt(yunList.get("payment_channel")) == 1) {
                            row1.add("微信");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 2) {
                            row1.add("支付宝");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 3) {
                            row1.add("paypal");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 4) {
                            row1.add("积分兑换");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 5) {
                            row1.add("线下付款");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 6) {
                            row1.add("微信+积分");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 7) {
                            row1.add("支付宝+积分");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 8) {
                            row1.add("微信+贝");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 9) {
                            row1.add("支付宝+贝");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 10) {
                            row1.add("购物券");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 11) {
                            row1.add("支付宝+购物券");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 12) {
                            row1.add("微信+购物券");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 15) {
                            row1.add("微信+线下支付");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 16) {
                            row1.add("支付宝+线下支付");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 17) {
                            row1.add("银联支付");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 18) {
                            row1.add("银联+购物券");
                        }else if (Convert.toInt(yunList.get("payment_channel")) == 104) {
                            row1.add("货款支付");
                        } else if (Convert.toInt(yunList.get("payment_channel")) == 19) {
                            row1.add("杉德支付");
                        }else {
                            row1.add("其他");
                        }
                    } else {
                        row1.add("");
                    }
                } else {
                    row1.add("");
                }
                row1.add(yunList.get("user_id"));
                row1.add(yunList.get("consigneeName"));
                row1.add(yunList.get("address"));
                row1.add(yunList.get("consigneePhone"));
                row1.add(yunList.get("create_time"));
                row1.add(yunList.get("pay_time"));
                row1.add(yunList.get("confirm_receipt_time"));
                map.put("serial_number", yunList.get("serial_number"));
                rows.add(row1);
            }
            data.setRows(rows);
            ExportExcelUtils.exportExcel(response, "云贸商城商家订单导出.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
